package com.yb0os1.adapter.reids2etcd.controller;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/redis")
/***
 * 适配器模式该节任务：使用适配器模式，将redis的适配为etcd,而且不要修改代码
 *  使用redis的API 但是底层调用的是etcd的实现，把我们的组件注册到spring 使用我们的组件替换redisTemplate
 */
public class KVController {
    private final StringRedisTemplate redisTemplate;

    public KVController(StringRedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }


    @GetMapping("/get/{key}")
    public String get(@PathVariable String key) {
        String value = redisTemplate.opsForValue().get(key);
        if(value==null)return "null";
        return value;
    }

    @PutMapping("/set/{key}/{value}")
    public String set(@PathVariable String key, @PathVariable String value) {
        redisTemplate.opsForValue().set(key, value);
        return "success";
    }
}
